package com.tc.itfarm.commons.web.security;

import java.util.Collection;
import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
/**
 * 
 * @Description:   检查用户是否够权限访问资源
 * @author: wangdongdong  
 * @date:   2016年7月8日 上午10:15:10   
 *
 */
@Service("commonAccessDecisionManager")
public class CommonAccessDecisionManager implements AccessDecisionManager {
	

	private Logger logger = LoggerFactory.getLogger(CommonAccessDecisionManager.class);
	
	// 检查用户是否够权限访问资源
	// 参数authentication是从spring的全局缓存SecurityContextHolder中拿到的，里面是用户的权限信息
	// 参数object是url
	// 参数configAttributes所需的权限

	public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes)
			throws AccessDeniedException, InsufficientAuthenticationException {
		if(configAttributes == null){
			return;
		}
		Iterator<ConfigAttribute> it = configAttributes.iterator();
		while(it.hasNext()){
			ConfigAttribute ca = it.next();
			String needRole = ((SecurityConfig) ca).getAttribute();
			for(GrantedAuthority ga : authentication.getAuthorities()){
				if(needRole.equals(ga.getAuthority())){
					return;
				}
			}
		}
		throw new AccessDeniedException("no right");
	}

	public boolean supports(ConfigAttribute attribute) {
		return true;
	}

	public boolean supports(Class<?> clazz) {
		return true;
	}

}
